package edu.kit.ksri.db.mturk.tests;

import java.util.ArrayList;

import edu.kit.ksri.db.mturk.plugins.performance.SimpleLinearForecast;
import edu.kit.ksri.db.mturk.utils.NumberEvaluation;
import edu.kit.ksri.db.mturk.utils.numberevaluationhelpers.AbstractInterval;
import edu.kit.ksri.db.mturk.utils.numberevaluationhelpers.HalfInterval;
import edu.kit.ksri.db.mturk.utils.numberevaluationhelpers.Interval;
import edu.kit.ksri.db.mturk.workerpools.WorkerPool;

/**
 * Copyright (c) 2010-2011 Karlsruhe Institute of Technology (KIT), Germany
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 * 
 * --------------------------------------------------------------------------
 * 
 * Software: CSP/WMV tool for dynamic quality management of microtasks
 * http://www.ksri.kit.edu/Upload/Users/PeopleClouds/HCOMP2011_An_extendable_toolkit_for_managing_quality_of_human-based_eServices.pdf
 * 
 * Organization: Karlsruhe Institute of Technology (KIT), Germany
 * http://www.kit.edu
 * 
 * Project: PeopleClouds project of the Karlsruhe Service Research Institute(KSRI)
 * http://www.ksri.kit.edu/Default.aspx?PageId=818
 * 
 * Contributing authors: David Bermbach, Sandra Rath, Pascal Wichmann, Robert Kern
 */
public class SimulationStarter {

	/** number of simulationthreads */
	private static int numberOfThreads = 200;

	public static void main(String[] args) throws Exception {
		init();
		SimulationThread[] threads = new SimulationThread[numberOfThreads];
		Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
		for (int i = 0; i < threads.length; i++) {
			threads[i] = new SimulationThread(new SimpleLinearForecast(), 1,
					1000, 50, 10, 100000, 100, 100, 1, 15);
			new Thread(threads[i]).start();
			Thread.yield();
		}
		ArrayList<AbstractInterval> intervals = createIntervals();
		// get results based on threads[i].isDone()
		NumberEvaluation n = new NumberEvaluation(intervals, new double [0]);
		System.out.println(intervals);
		int doneCounter = 0;
		boolean flag = false;
		while (!flag) {
			System.out.println("> Got " + doneCounter + " of " + threads.length
					+ " results, sleeping 1 second");
			Thread.sleep(1000);
			boolean foundThreadNotNull = false;
			for(int i = 0; i <threads.length;i++){
				if(threads[i]==null) continue;
				foundThreadNotNull = true;
				if(threads[i].isDone()){
					n.addValue(threads[i].getPercentage());
					threads[i]=null;
					doneCounter++;
				}
				
			}
			if(!foundThreadNotNull) break;
		}
		System.out.print("> Evaluating data...");
		n.evaluate();
		System.out.println("done");
		String res = n.getResultsAsCSV();
		System.out.println(res);
	}
	
	/**
	 * @return intervals from < 50%, via intervals [x,y) in steps of 5% until
	 *         >=150%
	 */
	private static ArrayList<AbstractInterval> createIntervals() {
		ArrayList<AbstractInterval> intervals = new ArrayList<AbstractInterval>();
		intervals.add(new HalfInterval(0.5, true, false));
		int i = 5;
		while(i<150) {
			intervals
					.add(new Interval(i, (i + 5) , false, true));
			i += 5;
		}
		intervals.add(new HalfInterval(150, false, true));
		return intervals;
	}
	
	private static void init() throws Exception {
		WorkerPool.createNewWorkerPool("ACTIVE",0);
		WorkerPool.createNewWorkerPool("PENDING",0);
		WorkerPool.createNewWorkerPool("UNKNOWN",0);
		WorkerPool.createNewWorkerPool("BLOCKED",0);
	}
	
}
